﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.Forms
{
    public partial class InspectionDepartmentForm : RelationsLMP.Control.Form.MetroDemoForms
    {

        private UC.SelectImage SelectImage;

        public Model.DB.tb_SubmitUnit SubmitUnit { get; set; }

        public InspectionDepartmentForm()
        {
            InitializeComponent();


            SelectImage = new UC.SelectImage();
            SelectImage.Dock = DockStyle.Fill;
            grpImage.Controls.Add(SelectImage);

            okReturnToolbar21.Event_OKClick += OkReturnToolbar21_Event_OKClick;
            okReturnToolbar21.Event_ReturnClick += OkReturnToolbar21_Event_ReturnClick;


            SubmitUnit = new Model.DB.tb_SubmitUnit();
        }

        public void ModifyInfo(Model.DB.tb_SubmitUnit submitUnit)
        {
            SubmitUnit.Id = submitUnit.Id;
            SubmitUnit.Name = submitUnit.Name;

            txtName.Text = submitUnit.Name;
            txtLatitude.Text = submitUnit.Latitude;
            txtLongitude.Text = submitUnit.Longitude;
            txtGPSDesc.Text = submitUnit.GPSDesc;

            if (!string.IsNullOrEmpty(submitUnit.ImageInfo))
            {
                Bitmap _bitmap = Static.Methods.GetImage(Model.SystemEnum.ImageType.SubmitUnit, SubmitUnit.Id.ToString());
                if (_bitmap != null)
                {
                    SelectImage.PanelImage = _bitmap;
                }
                else
                    SelectImage.PanelImage = null;
            }
        }

        private void OkReturnToolbar21_Event_OKClick(object sender, EventArgs e)
        {

            if (string.IsNullOrWhiteSpace(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show(this, "【单位名称】不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (txtName.Text != SubmitUnit.Name && new DataAccess.tb_SubmitUnit().CheckSubmitUnitIsExist(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show(this, "【单位名称】已存在！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SubmitUnit = new Model.DB.tb_SubmitUnit();
            SubmitUnit.Name = txtName.Text;
            SubmitUnit.Longitude = txtLongitude.Text;
            SubmitUnit.Latitude = txtLatitude.Text;
            SubmitUnit.GPSDesc = txtGPSDesc.Text;

            //SubmitUnit.ImageInfo = SelectImage.ImageString;
            if (SelectImage.PanelImage != null)
                SubmitUnit.ImageInfo = Relations.Common.Methods.ImageToBase64String(SelectImage.PanelImage);
            else
                SubmitUnit.ImageInfo = string.Empty;


            DialogResult = DialogResult.OK;
        }

        private void OkReturnToolbar21_Event_ReturnClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
